/**
 * Create by CaoWenjian
 *
 * @copyRight: 2018
 **/
package com.bjboot.caocao.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 *@Description 日期处理工具类
 *@Author 操文健
 *@Date 2018-09-04 上午8:19
 *@Version 1.0
 **/
public class DateUtils {
    private final static Logger logger = LoggerFactory.getLogger(DateUtils.class);
    /**
     * 时间格式(yyyy-MM-dd)
     */
    public final static String DATE_PATTERN = "yyyy-MM-dd";
    /**
     * 时间格式(yyyy-MM-dd HH:mm:ss)
     */
    public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static String format(Date date) {
        return format(date, DATE_PATTERN);
    }

    public static String format(Date date, String pattern) {
        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            return df.format(date);
        }
        return null;
    }

    /**
     * 计算距离现在多久，非精确
     *
     * @param date
     * @return
     */
    public static String getTimeBefore(Date date) {
        Date now = new Date();
        long l = now.getTime() - date.getTime();
        long day = l / (24 * 60 * 60 * 1000);
        long hour = (l / (60 * 60 * 1000) - day * 24);
        long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        String r = "";
        if (day > 0) {
            r += day + "天";
        } else if (hour > 0) {
            r += hour + "小时";
        } else if (min > 0) {
            r += min + "分";
        } else if (s > 0) {
            r += s + "秒";
        }
        r += "前";
        return r;
    }

    /**
     * 计算距离现在多久，精确
     *
     * @param date
     * @return
     */
    public static String getTimeBeforeAccurate(Date date) {
        Date now = new Date();
        long l = now.getTime() - date.getTime();
        long day = l / (24 * 60 * 60 * 1000);
        long hour = (l / (60 * 60 * 1000) - day * 24);
        long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        String r = "";
        if (day > 0) {
            r += day + "天";
        }
        if (hour > 0) {
            r += hour + "小时";
        }
        if (min > 0) {
            r += min + "分";
        }
        if (s > 0) {
            r += s + "秒";
        }
        r += "前";
        return r;
    }

    /**
     * 对传入的Year、Month、Day进行组装并格式化，并且如果Day大于本月最大日期，则重置Day为本月最后一天
     *
     * @param tYear
     * @param tMonth
     * @param tDay
     * @return GregorianCalendar
     */
    public static GregorianCalendar getFormatDate(int tYear, int tMonth, int tDay) {
        GregorianCalendar tCalendar = new GregorianCalendar();
        int arrComnYearEndDate[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        int arrLeapYearEndDate[] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        tCalendar.set(Calendar.YEAR, tYear);
        tCalendar.set(Calendar.MONTH, tMonth);
        if (isLeapYear(tYear + tMonth / 11) && tDay > arrLeapYearEndDate[tMonth % 11]) {
            tCalendar.set(Calendar.DATE, arrLeapYearEndDate[tMonth % 11]);
        } else if (!isLeapYear(tYear + tMonth / 11) && tDay > arrComnYearEndDate[tMonth % 11]) {
            tCalendar.set(Calendar.DATE, arrComnYearEndDate[tMonth % 11]);
        } else {
            tCalendar.set(Calendar.DATE, tDay);
        }
        return tCalendar;
    }

    /**
     * 判断是否为闰年
     */
    public static boolean isLeapYear(int nYear) {
        boolean ResultLeap = false;
        ResultLeap = (nYear % 400 == 0) | (nYear % 100 != 0) & (nYear % 4 == 0);
        return ResultLeap;
    }

    /**
     * 将字符串转换为Date类型
     *
     * @param date
     *            字符串类型
     * @param pattern
     *            格式
     * @return 日期类型
     */
    public static Date format(String date, String pattern) {
        if (pattern == null || pattern.equals("") || pattern.equals("null")) {
            pattern = "yyyy-MM-dd HH:mm:ss";
        }
        if (date == null || date.equals("") || date.equals("null")) {
            return new Date();
        }
        Date d = null;
        try {
            d = new java.text.SimpleDateFormat(pattern).parse(date);
        } catch (ParseException pe) {
        }
        return d;
    }

    /**
     * 得到当前系统日期
     *
     * @return 当前日期的格式字符串,日期格式为"yyyyMMddHH:mm:ss"
     */
    public static String getCurrentDateMi() {
        String pattern = "yyyyMMddHH:mm:ss";
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        Date today = new Date();
        Date newDate = new Date(today.getTime() + 7 * 24 * 3600000);
        String tString = df.format(newDate);
        return tString;
    }

    /**
     * 得到当前系统日期
     *
     * @return 当前日期的格式字符串,日期格式为"yyyy-MM-dd"
     */
    public static String getCurrentDate() {
        String pattern = "yyyy-MM-dd";
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        Date today = new Date();
        String tString = df.format(today);
        return tString;
    }

    /**
     * 得到当前系统时间
     *
     * @return 当前时间的格式字符串，时间格式为"HH:mm:ss"
     */
    public static String getCurrentTime() {
        String pattern = "HH:mm:ss";
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        Date today = new Date();
        String tString = df.format(today);
        return tString;
    }

    /**
     * 得到昨天的日期
     */

    public static String getYesterdayDate() {
        String pattern = "yyyy-MM-dd";
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        Calendar yesterday = Calendar.getInstance();
        yesterday.add(Calendar.DATE, -1);
        String tString = df.format(yesterday.getTime());
        return tString;
    }

    /**
     * 得到最近一周前的日期
     */

    public static String getLastWeekDayDate() {
        String pattern = "yyyy-MM-dd";
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        Calendar lastWeekDay = Calendar.getInstance();
        lastWeekDay.add(Calendar.DATE, -7);
        String tString = df.format(lastWeekDay.getTime());
        return tString;
    }

    /**
     * 得到最近一月前的日期
     */

    public static String getLastMonthDate() {
        String pattern = "yyyy-MM-dd";
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        Calendar lastMonth = Calendar.getInstance();
        lastMonth.add(Calendar.DATE, -30);
        String tString = df.format(lastMonth.getTime());
        return tString;
    }

    /**
     * 取得某个日起时间的的时间
     *
     */
    public static String getsomeTimeBySomeDate(Date datetime) {
        String pattern = "HH:mm:ss";
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        String tString = df.format(datetime);
        return tString;
    }

    /**
     * 将字符串转化为时间 字符型转化时间 时间格式为：yyyy-MM-dd
     *
     * @param temp
     * @return Date型转换结果
     */
    public static Date str2Date(String temp) {
        Date date = null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            if (!("".equals(temp) || temp == null)) {
                date = simpleDateFormat.parse(temp);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 以baseDate为基准向前或向后推interval个时间单位的时间
     *
     * @param baseDate
     * @param interval
     * @param unit
     *            例如：Calendar.DATE
     * @return
     */
    public static Date calNextDate(Date baseDate, int interval, int unit) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(baseDate);
        calendar.add(unit, interval);
        return calendar.getTime();
    }

    /**
     * 以baseDate为基准向前或向后推interval个时间单位的时间 ，默认为日
     *
     * @param baseDate
     * @param interval
     * @return
     */
    public static Date calNextDate(Date baseDate, int interval) {
        return calNextDate(baseDate, interval, Calendar.DATE);
    }
}
